/**
 * @author Jason Pratt
 */

package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

public class VariablesViewController extends edu.cmu.cs.stage3.alice.authoringtool.util.GroupingPanel implements edu.cmu.cs.stage3.alice.authoringtool.util.GUIElement, edu.cmu.cs.stage3.alice.authoringtool.util.Releasable {

 protected edu.cmu.cs.stage3.alice.core.property.ObjectArrayProperty variables;
 protected RefreshListener refreshListener = new RefreshListener();
 // protected SyncListener syncListener = new SyncListener();
 protected boolean sleeping = false;

 public VariablesViewController () {
  setOpaque( false );
  setLayout( new javax.swing.BoxLayout( this, javax.swing.BoxLayout.X_AXIS ) );
  setBorder( null );
  }

 public void set (edu.cmu.cs.stage3.alice.core.property.ObjectArrayProperty variables) {
  clean();
  this.variables = variables;
  if (! sleeping) {
   startListening();
   }
  refreshGUI();
  }

 public void goToSleep () {
  if (! sleeping) {
   stopListening();
   sleeping = true;
   }
  }

 public void wakeUp () {
  if (sleeping) {
   startListening();
   sleeping = false;
   }
  }

 public void die () {
  clean();
  }

 public void clean () {
  stopListening();
  removeAll();
  variables = null;
  }

 //Rem @Override
 public void release () {
  edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.releaseGUI( this );
  }

 public void refreshGUI () {
  removeAll();
  Object[] vars = variables.getArrayValue();
  //LIB for (Object var : vars)
  int t_tam = vars!=null ? vars.length : 0;
  for (int i_i=0; i_i<t_tam; i_i++) {
   final edu.cmu.cs.stage3.alice.core.Variable variable = (edu.cmu.cs.stage3.alice.core.Variable)vars[i_i]; //L (edu.cmu.cs.stage3.alice.core.Variable)var;
   if (variable != null) {
    javax.swing.JComponent gui = null;
    edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory setImmediatelyFactory = new edu.cmu.cs.stage3.alice.authoringtool.util.SetPropertyImmediatelyFactory( variable.value );
    gui = edu.cmu.cs.stage3.alice.authoringtool.util.GUIFactory.getPropertyViewController( variable.value, true, true, false, setImmediatelyFactory );
    add( gui );
    }
   }
  }

 protected void startListening () {
  if (variables != null) {
   variables.addObjectArrayPropertyListener( refreshListener );
   Object[] vars = variables.getArrayValue();
   //LOB for (Object var : vars)
   int t_tam = vars!=null ? vars.length : 0;
   for (int i_i=0; i_i<t_tam; i_i++) {
    if (vars[i_i] != null) { //L if (var != null )
     ((edu.cmu.cs.stage3.alice.core.Variable)vars[i_i]).name.addPropertyListener( refreshListener );//L ((edu.cmu.cs.stage3.alice.core.Variable)var)....
     }
    }
   }
  }

 protected void stopListening () {
  if (variables != null) {
   variables.removeObjectArrayPropertyListener( refreshListener );
   Object[] vars = variables.getArrayValue();
   //LOB for (Object var : vars)
   int t_tam = vars!=null ? vars.length : 0;
   for (int i_i=0; i_i<t_tam; i_i++) {
    if (vars[i_i] != null) { //L if (var != null )
     ((edu.cmu.cs.stage3.alice.core.Variable)vars[i_i]).name.removePropertyListener( refreshListener ); //L ((edu.cmu.cs.stage3.alice.core.Variable)var)...
     }
    }
   }
  }

 class RefreshListener implements edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyListener, edu.cmu.cs.stage3.alice.core.event.PropertyListener {
  public void objectArrayPropertyChanging (edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent ev) {}
  public void objectArrayPropertyChanged (edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent ev) {
   if (ev.getChangeType() == edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent.ITEM_INSERTED) {
    edu.cmu.cs.stage3.alice.core.Variable variable = (edu.cmu.cs.stage3.alice.core.Variable)ev.getItem();
    if (variable != null) {
     variable.name.addPropertyListener( this );
     }
   } else if (ev.getChangeType() == edu.cmu.cs.stage3.alice.core.event.ObjectArrayPropertyEvent.ITEM_REMOVED) {
    edu.cmu.cs.stage3.alice.core.Variable variable = (edu.cmu.cs.stage3.alice.core.Variable)ev.getItem();
    if (variable != null) {
     variable.name.removePropertyListener( this );
     }
    }
   VariablesViewController.this.refreshGUI();
   }
  public void propertyChanging( edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev) {}
  public void propertyChanged( edu.cmu.cs.stage3.alice.core.event.PropertyEvent ev) {
   VariablesViewController.this.refreshGUI();
   }
  }

 }
